<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>滴答办公系统-部门列表</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport"
		  content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="media/layui/css/layui.css" media="all">
	<script src="media/js/jquery.min.js"></script>
</head>
<body>
<div class="layui-container">
	<table class="layui-table" id="tbdata" lay-filter="tbop">
		<thead>
		<tr>
			<td>序号</td>
			<td>部门名称</td>
			<td>部门人数</td>
			<td>成立日期</td>
			<td>操作</td>
		</tr>
		</thead>
		<tbody id="tbody">



		</tbody>
	</table>
	<div class="layui-box layui-laypage layui-laypage-default" id="layui-laypage-1" id="pagebutton">
		<a id="lasta"  href="javascript:;" class="layui-laypage-prev layui-disabled" data-page="0">
			<i class="layui-icon">&lt;</i>
		</a>

        <a id="a1">1</a>
        <a id="a2">2</a>

		<a id="nexta"  href="javascript:;" class="layui-laypage-next layui-disabled" data-page="2">
			<i class="layui-icon">&gt;</i>
		</a>
		<span class="layui-laypage-skip">到第
							   <input type="text" min="1" value="1" class="layui-input" id="page">页
								<button type="button" class="layui-laypage-btn" id="abutton">确定</button>
							</span>
		<span class="layui-laypage-count">共 6 条</span>

	</div>
</div>
<script src="media/layui/layui.js"></script>

<script type="text/javascript">

	$(function () {
		queryDepart("/queryDepart/1/5");


		$("#lasta").click(function () {
			queryDepart("/queryDepart/1/5");
		})

        $("#a1").click(function () {
            queryDepart("/queryDepart/1/5");
        })

        $("#a2").click(function () {
            queryDepart("/queryDepart/2/5");
        })

		$("#nexta").click(function () {
			queryDepart("/queryDepart/2/5");
		})

        $("#abutton").click(function () {
            var num = $("#page").val();
            if (num>2){
                num = 2;
                $("#page").val(num);
            }
            if (num<1){
                num = 1;
                $("#page").val(num);
            }
            queryDepart("/queryDepart/"+num+"/5");
        })

		function queryDepart(url){
			$.ajax({
				url:url,
				success:function (data) {
					var a = JSON.parse(data);
					$("#tbody tr").remove();
					for (var i = 0;i < a.records.length;i++){
						var d = a.records[i];

						$("#tbody").append("<tr>"+
								"<td>"+d.id+"</td>"+
								"<td>"+d.name+"</td>"+
								"<td>测试</td>" +
								"<td>"+d.createtime+"</td>" +
								"<td><a class=\"layui-btn layui-btn-mini\" onclick='updateDepart("+d.id+")'>编辑</a>" +
								"<a class='layui-btn layui-btn-danger layui-btn-mini'  onclick='deleteDepart("+d.id+")'>删除</a></td></tr>");

					}
				}

			})
		}

	})


	function deleteDepart(id){
		layui.use('table', function() {
			layer.confirm('是否确认删除部门?',function(index) {
				$.ajax({
					url:"/deleteDepart",
					data:{id:id},
					success:function (data) {

						var d = JSON.parse(data)
						if (d.code==100){
							layer.msg("删除成功", {icon : 6});
							setTimeout(function(){
								location.reload();
							},1000)
						}else{
							layer.msg("删除失败", {icon : 5});
						}
					}
				})
			});
		});
	}

	function updateDepart(i) {
		window.location.href = "departupdate.html?id="+i;
	}
</script>


</body>
</html>